﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class DALBatteryManufacturer : IDALBatteryManufacturer, IDisposable
    {
        private CarBatteryEntities context;
        private bool disposed = false;

        public DALBatteryManufacturer(CarBatteryEntities context)
        {
            this.context = context;
        }

        public IEnumerable<BatteryManufacturer> GetBatteryManufacturers()
        {
            return context.BatteryManufacturer.AsEnumerable();
        }

        public BatteryManufacturer GetBatteryManufacturerByID(int bManID)
        {
            return context.BatteryManufacturer.Find(bManID);
        }

        public BatteryManufacturer GetBatteryManufacturerByName(string bManName)
        {
            return context.BatteryManufacturer.FirstOrDefault(x => x.name.Equals(bManName));
        }

        public void InsertBatteryManufacturer(BatteryManufacturer bMan)
        {
            context.BatteryManufacturer.Add(bMan);
        }

        public void DeleteBatteryManufacturer(int bManID)
        {
            context.BatteryManufacturer.Remove(GetBatteryManufacturerByID(bManID));
        }

        public void UpdateBatteryManufacturer(BatteryManufacturer bMan)
        {
            context.Entry(bMan).State = EntityState.Modified;
        }

        public int Save()
        {
            return context.SaveChanges();
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                    context.Dispose();
            }

            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}